package fz.vrd.library.page;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import fz.vrd.library.log.LogFactory;
import fz.vrd.library.permissions.PermissionsManage;
import fz.vrd.library.utils.AppUtils;
import fz.vrd.library.utils.StringUtils;


/**
 * <b>类名称：父类activity和Fragment，通用基础方法  <br/>
 * <b>创建人： Administrator <br/>
 * <b>时间： 2021/6/10 10:11 <br/>
 * <b>说明：{ } <br/>
 */
public abstract class Page {

    PermissionsManage permissionsManage;

    private ActivityResultLauncher<Intent> activityResultLauncher;

    public abstract FragmentActivity getActivity();

    public abstract Context getContext();

    /**
     * 返回上一个页面(手机的物理返回键也需要调用这个方法)
     */
    public abstract void backPage();

    /**
     * 替代 startActivityForResult
     */
    public void registerForActivityResult(ActivityResultCallback<ActivityResult> activityResultCallback){
        activityResultLauncher = getActivity().registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),activityResultCallback);
    }

    /**
     * 获取手机屏幕宽高
     */
    public int getPhoneWidth() {
        return AppUtils.getPhoneW(getContext());
    }

    public int getPhoneHeight() {
        return AppUtils.getPhoneH(getContext());
    }


    /**
     * 是否允许全屏
     */
    public boolean isCurPageFullScreen() {
        return (getActivity().getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == WindowManager.LayoutParams.FLAG_FULLSCREEN;
    }

    /**
     * 是否允许旋转屏幕
     */
    public boolean isAllowScreenRoate() {
        return (android.provider.Settings.System.getInt(getActivity().getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1);
    }

    /**
     * 是否是竖屏显示
     */
    public boolean isPortraitScreen() {
        return getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }


    /**
     * 屏幕显示方式(竖屏/横屏)
     *
     * @param isPortrait true :竖屏  false:横屏
     */
    public void setScreenShowMode(boolean isPortrait) {
        if (isPortrait) {
            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
        } else {
            getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        }
    }

    public void log(String msg) {
        LogFactory.getInstance().d(msg);
    }

    public void log(String tag, String msg) {
        LogFactory.getInstance().d(tag, msg);
    }

    /**
     * 软键盘的隐藏显示
     */
    public void showInputMethod(View view) {
        if (getActivity().getCurrentFocus() != null) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
            if (view != null) {
                imm.showSoftInput(view, 0);
            } else {
                imm.showSoftInputFromInputMethod(getActivity().getCurrentFocus().getWindowToken(), 0);
            }
        }
    }

    public void hideSoftInput() {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
        if (getActivity().getCurrentFocus() != null) {
            imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
        }
    }

    public boolean isEmpty(CharSequence... arg) {
        return StringUtils.isNull(arg);
    }

    public boolean isNum(String num) {
        return StringUtils.isNum(num);
    }

    public PermissionsManage getPermissionsManage() {
        if (permissionsManage == null) {
            permissionsManage = new PermissionsManage();
            permissionsManage.setActivity(getActivity());
        }
        return permissionsManage;
    }

    public void gotoAct(Class<?> clas) {
        gotoAct(null,null,clas);
    }

    public void gotoAct(String key, String value, Class<?> clas) {
        gotoAct(key,value,null,clas,null,-1);
    }

    public void gotoAct(Bundle bundle, Class<?> clas) {
        gotoAct(null,null,bundle,clas,null,-1);
    }


    public void gotoAct(Intent intent) {
        gotoAct(intent,-1);
    }

    public void gotoAct(Intent intent,int requestCode) {

        gotoAct(null,null,null,null,intent,requestCode);
    }

    public void gotoAct(String key, String value,Bundle bundle, Class<?> clas,Intent intent,int requestCode) {

        if(intent == null){
            intent = new Intent();
        }

        if(clas != null){
            intent.setClass(getActivity(), clas);
        }

        if(!isEmpty(key,value)){
            intent.putExtra(key, value);
        }
        if(bundle != null){
            intent.putExtras(bundle);
        }
        if(requestCode == -1){
            getActivity().startActivity(intent);
        }else{
            if(Build.VERSION.SDK_INT > Build.VERSION_CODES.Q){
                if(activityResultLauncher != null){
                    activityResultLauncher.launch(intent);
                }
            }else{
                getActivity().startActivityForResult(intent,requestCode);
            }
        }
    }




}
